const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')

Page(Monitor.hookApp({

  data: {
    waysArr:[],
    qingdan: `${getApp().globalData.imgUrl}jsy-newshop清单.svg`,
    quxiao: `${getApp().globalData.imgUrl}jsy-newshop已取消复制.svg`,
    imgUrls: getApp().globalData.imgUrls
  },
  onLoad(o) {

    this.setData({
      order_id: o.order_id,
      distributiontype: o.distributiontype
    })
  },
  onShow() {
    this.getPageInfo()
  },
  specifications(trg) { //商品规格转换
    let len = trg.length
    for (let i = 0; i < len; i++) {
      let size = ''
      let str = `${trg[i].sku_type}`
      trg[i].sku_type = JSON.parse(str)
      for (let j = 0; j < trg[i].sku_type.length; j++) {
        for (let prop in trg[i].sku_type[j]) {
          size += trg[i].sku_type[j][prop] + " / "
        }
      }
      trg[i].size = size
      trg[i].size = trg[i].size.substring(0, trg[i].size.length - 3)
    }
    return trg
  },
  copyText(e) { //点击复制
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  getPageInfo() {
    util.API('pyapi/oder_if', {
      order_id: this.data.order_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        address,
        address_info,
        code,
        data,
        info,
        distribution_name,
        orders_goods_list
      } = res
      if (res.code == 200) {
        let date = util.getTimeDian(data.ctime),
          date2 = util.getTimeDian(data.cancel_time),
          date1 = util.getTimeDian(data.paytime);
        data.ctime = `${date.y}.${date.m}.${date.d} ${date.h}:${date.i}:${date.s}`
        data.cancel_time = `${date2.y}.${date2.m}.${date2.d} ${date2.h}:${date2.i}:${date2.s}`
        data.ptime = `${date1.y}.${date1.m}.${date1.d} ${date1.h}:${date1.i}:${date1.s}`
        console.log(data,123321)
        let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
        orders_goods_list = this.specifications(orders_goods_list) // 改规格
        this.setData({
          waysArr,
          pageInfo: data,
          orderInfo:orders_goods_list
        })
      }
    })
  },
  // let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
  setWaysArr(waysArr, distribution_type, distribution_name) {  // 设置自定义配送信息
    distribution_type = distribution_type.split(',');
    distribution_name = JSON.parse(distribution_name)
    let len = distribution_type.length
    for (let i = 0; i < len; i++) {
      waysArr.push(distribution_name[distribution_type[i]])
    }
    return waysArr
  },
  cancellationOfOrder(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: e.currentTarget.dataset.status == 'shanchu' ? '是否确定删除订单' : '是否确定取消订单',
      success(res) {
        if (res.confirm) {
          util.API('pyapi/Cancellation_order', {
            order_id: e.currentTarget.dataset.id
          }, 'application/x-www-form-urlencoded').then(res => {
            if (res.code == 200) {
              wx.showToast({
                title: '订单删除成功',
                duration: 1000
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: `.pages/dingDanList/dingDanList?status=all`,
                })
              }, 1000)
            } else {
              wx.showToast({
                title: res.msg + '',
                icon: 'none'
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
}))